Imports System.Data

Partial Class pages_Orders_default : Inherits CPage

#region "Querystring (Filters)"
    Public ReadOnly Property Search As String
        Get
            Return CWeb.RequestStr("search")
        End Get
    End Property
    Public ReadOnly Property CustomerId As Integer
        Get
            Return CWeb.RequestInt("customerId")
        End Get
    End Property
    Public ReadOnly Property ProductId As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
    Public ReadOnly Property IsPaid As Boolean?
        Get
            Return CWeb.RequestBoolNullable("isPaid")
        End Get
    End Property
#End Region

#Region "Members"
    Private m_orders As COrderList
#End Region

#Region "Data"
    Public ReadOnly Property [Orders]() As COrderList
        Get
            If IsNothing(m_orders) Then
                m_orders = New COrder().SelectSearch(ctrlOrders.PagingInfo, txtSearch.Text, ProductId, CustomerId, IsPaid) 'Sql-based Paging
                'm_orders.PreloadChildren() 'Loads children for page in one hit (where applicable)
            End If
            Return m_orders
        End Get
    End Property
    Public Function OrdersAsDataset() As System.Data.DataSet
        Return New COrder().SelectSearch_Dataset(txtSearch.Text, ProductId, CustomerId, IsPaid)
    End Function

    Private m_paid As Decimal = Decimal.MinValue
    Private m_value As Decimal = Decimal.MinValue
    Private m_net As Decimal = Decimal.MinValue
    Public Function TotalPaid() As Decimal
        If m_paid = Decimal.MinValue Then
            m_paid = New COrder().SelectTotalPaid(txtSearch.Text, ProductId, CustomerId)
            If m_paid = 0 Then Return Decimal.MinValue
        End If
        Return m_paid
    End Function
    Public Function TotalNet() As Decimal
        If m_net = Decimal.MinValue Then
            m_net = New COrder().SelectTotalNet(txtSearch.Text, ProductId, CustomerId)
            If m_net = 0 Then Return Decimal.MinValue
        End If
        Return m_net
    End Function
    Public Function TotalValue() As Decimal
        If m_value = Decimal.MinValue Then
            m_value = New COrder().SelectTotalValue(txtSearch.Text, ProductId, CustomerId)
            If m_value = 0 Then Return Decimal.MinValue
        End If
        Return m_value
    End Function
    Public Function Percent() As Double
        If TotalValue() = 0 OrElse TotalPaid() = Decimal.MinValue Then Return Double.NaN
        Return 100.0 * TotalPaid() / TotalValue()
    End Function
#End Region

#Region "Event Handlers - Page "
    Protected Overrides Sub PageInit()

        'Populate Dropdowns
        If CustomerId > 0 Then
            UnbindSideMenu()
            PageTitle &= " for " & New CCustomer(CustomerId).CustomerName
            MenuSelected = "Customers"
            AddMenuSide("Customers", CSitemap.Customers)
            AddMenuSide("Customer Details", CSitemap.Customer(CustomerId))
            AddMenuSide("Customer Orders")
            'AddMenuSide("Customer Reminders", CSitemap.RemindersForCustomer(CustomerId))
            AddMenuSide("Customer Payments", CSitemap.PaymentsForCustomer(CustomerId))
        ElseIf ProductId > 0 Then
            ddCurrent.Visible = True

            UnbindSideMenu()
            PageTitle &= " for " & CProduct.Cache.GetById(ProductId).ProductName
            MenuSelected = "Products"
            With CProduct.Cache.GetById(ProductId)
                AddMenuSide(.ProductType.Plural & "...", CSitemap.Products(Nothing, .ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
                AddMenuSide(.TypeName & " Details", CSitemap.ProductEdit(ProductId))
                AddMenuSide("Image" & IIf(.HasImage, "*", String.Empty), CSitemap.ProductImage(ProductId))
                If .IsSet Then AddMenuSide(CUtilities.NameAndCount("Contents", .Children), CSitemap.ProductContents(ProductId))
                If .CanHaveTranscript Then
                    AddMenuSide("Transcription" & IIf(.HasTranscript, "*", String.Empty), CSitemap.ProductTranscription(ProductId))
                End If
                If .CanHaveFiles Then
                    AddMenuSide("Dropbox Files", CSitemap.ProductFiles(ProductId))
                Else
                    AddMenuSide(CUtilities.NameAndCount("Epub/Mobi eBooks", CFile.Cache.GetByProductId(Me.ProductId)), CSitemap.ProductFiles(ProductId))
                End If
                If Not .IsFree OrElse .PaymentsCount > 0 OrElse .OrdersCountCached > 0 Then
                    AddMenuSide(CUtilities.NameAndCount("Orders", .OrdersCountCached), CSitemap.OrdersForProduct(ProductId), True)
                    AddMenuSide(CUtilities.NameAndCount("Payments", .PaymentsCount), CSitemap.PaymentsForProduct(ProductId))
                End If
            End With
        End If

        CDropdown.Add(ddIsPaid, CUtilities.NameAndCount("Paid", New COrder().SelectCount("", ProductId, CustomerId, True)), "True")
        CDropdown.Add(ddIsPaid, CUtilities.NameAndCount("Unpaid", New COrder().SelectCount("", ProductId, CustomerId, False)), "False")
        CDropdown.BlankItem(ddIsPaid, "All")
        If IsPaid.HasValue Then CDropdown.SetValue(ddIsPaid, IsPaid.Value.ToString) Else ddIsPaid.SelectedIndex = 0

        'Search state (from querystring)
        txtSearch.Text = Me.Search

        'Display Results
        ctrlOrders.Display(Me.Orders, CustomerId > 0, ProductId > 0)
        ctrlOrders.TotalPaid = Me.TotalPaid
        ctrlOrders.TotalValue = Me.TotalValue
        ctrlOrders.TotalNet = Me.TotalNet
        ctrlOrders.Percent = Me.Percent

        'Client-side
        Me.Form.DefaultFocus = txtSearch.ClientID
        Me.Form.DefaultButton = btnSearch.UniqueID  'CTextbox.OnReturnPress(txtSearch, btnSearch)
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click, ddIsPaid.SelectedIndexChanged
        Dim isPaid As Boolean? = Nothing
        If ddIsPaid.SelectedIndex > 0 Then isPaid = True
        If ddIsPaid.SelectedIndex > 1 Then isPaid = False
        Response.Redirect(CSitemap.Orders(txtSearch.Text, ProductId, CustomerId, isPaid))
    End Sub
#End Region

#Region "Event Handlers - UserControl"
    Private Sub ctrl_ExportClick() Handles ctrlOrders.ExportClick
        CDataSrc.ExportToCsv(OrdersAsDataSet, Response, "Orders.csv")
    End Sub
    Private Sub ctrl_ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer) Handles ctrlOrders.ResortClick
        Response.Redirect(CSitemap.Orders(Search, ProductId, CustomerId, IsPaid, New CPagingInfo(0, pageNumber - 1, sortBy, descending)))
    End Sub
#End Region

End Class
